package com.atguigu.yygh.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.msm.service.MsmService;
import com.atguigu.yygh.msm.utils.RandomUtil;
import com.atguigu.yygh.vo.msm.MsmVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    /**
     * 发送验证码短信
     * @param phone 电话号/手机号
     * @param param
     * @return
     */
    @Override
    public boolean send(String phone, Map<String, Object> param) {
        //1、判断手机号是否为空
        if(StringUtils.isEmpty(phone)){
            return false;
        }
        //2、创建客户端对象  (当前访问接口用户信息)
        DefaultProfile profile = DefaultProfile.getProfile(
                "default",//默认模式
                "LTAI5tKnMW69W5Be7AJ3y9HB", //id
                "WlbFSrZXstNUKZ0528odf8hON6kr7u"//密钥
        );
        IAcsClient client = new DefaultAcsClient(profile);//将配置信息传入默认Acs客户端
        //3、创建request请求对象，并给对象设置参数
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone); //手机号
        request.putQueryParameter("SignName", "我的谷粒在线教育网站");  //签名名称
        request.putQueryParameter("TemplateCode", "SMS_272555887");  //模板编号
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));  //模板参数

        try {
            //4、客户端调用方法发送请求，获取响应结果
            CommonResponse response = client.getCommonResponse(request);
            //5、从响应中 获得结果返回
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();//结果返回
        } catch (ClientException e) { //客户端异常
            e.printStackTrace();
            throw new YyghException(20001,"发送短信失败");
        }

    }

    /**
     * 发送短信实现
     * @param msmVo
     * @return
     */
    @Override
    public boolean send(MsmVo msmVo) {
//        if(!StringUtils.isEmpty(msmVo.getPhone())) {
            //仅为了测试
            //String code = (String)msmVo.getParam().get("code");
//            String code = RandomUtil.getFourBitRandom();
//            return this.sendMsmDx(msmVo.getPhone(),code);
//        }
        //1、获取参数
        String phone = msmVo.getPhone();
        String templateCode = msmVo.getTemplateCode();
        Map<String, Object> param = msmVo.getParam();
        //2、模拟发送通知短信
        String code = RandomUtil.getFourBitRandom();
        Map<String,Object> param1 = new HashMap<>();
        param1.put("code",code);
        //4、调用方法发送验证码短信
        boolean isSend=true;
        return isSend;
    }
}